import logo from '@/assets/logo.png'
import './index.scss'
import { Card, Form, Input, Button, Checkbox, message } from 'antd'
import { useDispatch } from 'react-redux'
import {fetchLogin} from '@/store/modules/user'
import { useNavigate } from 'react-router-dom'
const FormField = () => {
  const navigate = useNavigate()
  const dispatch = useDispatch()
  const onFinish = async (values) => {
    // console.log('Success:', values)
    // 触发异步Action fetchLogin
    await dispatch(fetchLogin(values))
    navigate('/')
    message.success(values)
  }
  const onFinishFailed = (errorInfo) => {
    console.log('Failed:', errorInfo)
    message.error(errorInfo)
  }
  return (
    <Form onFinish={onFinish} onFinishFailed={onFinishFailed}>
      <Form.Item name="mobile"
      validateTrigger={['onBlur', 'onChange']}
      rules={[
        {
          required: true,
          message: 'Please input your mobile!',
        },
        {
          pattern: /^1[3-9]\d{9}/,
          message: '手机号格式有误'
        }
      ]}>
        <Input size="large" placeholder="请输入手机号" />
      </Form.Item>
      <Form.Item name="code"
      validateTrigger={['onBlur']}
      rules={[
        {
          required: true,
          message: 'Please input your code!',
        }
      ]}>
        <Input size="large" placeholder="请输入验证码" />
      </Form.Item>
      <Form.Item>
        <Checkbox className="login-checkbox-label">
          我已阅读并同意「用户协议」和「隐私条款」
        </Checkbox>
      </Form.Item>
      <Form.Item>
        <Button type="primary" htmlType="submit" size="large" block>
          登录
        </Button>
      </Form.Item>
    </Form>
  )
}
const Login = () => {
  return (
    <div className="login">
      <Card className="login-container">
        <img className="login-logo" src={logo} alt="" />
        {/* 登录表单 */}
        <FormField />
      </Card>
    </div>
  )
}

export default Login